/******************************************************************************
 * @file bsp_led_driver.c
 *
 * @par dependencies
 *  - bsp_led_driver.h
 *
 * @brief Provides the APIs of LEDs and corresponding operations
 *
 * Processing flow:
 *
 * call directly.
 *
 * @author iceiceiceberg
 *
 * @date 2025-05-11
 *
 * @version V1.0.0
 *
 * @note 1 tab == 4 spaces
 *
 *****************************************************************************/

//********************************* Includes ********************************//
#include "bsp_led_driver.h"

//********************************* Includes ********************************//

//********************************* Defines *********************************//
/**
 * @brief Instantiates the bsp_led_driver_t target.
 * 
 * Steps:
 *  1. Initializes the LED driver instance.
 *  
 * @param[in] self        : Pointer to the target of driver.
 * 
 * @return led_state_t : Status of the function.
 * 
 * */
static led_state_t bsp_led_driver_init( bsp_led_driver_t    * const self ) 
{
    led_state_t     ret        = LED_OK;

/************** 1.Checking the input params **************/
    if( NULL == self )
    {
        ret = LED_ERROR_PARAM;
#ifdef DEBUG
        DEBUG_PRINT("LED_ERROR_PARAM\r\n");
#endif // DEBUG

        return ret;
    }
    
/************** 2.Checking the resource ******************/
    if( LED_IS_INSTED == self->is_instantiated )
    {
        ret = LED_ERROR_RESOURCE;
#ifdef DEBUG
        DEBUG_PRINT("LED_ERROR_RESOURCE at led_driver_init\r\n");
#endif // DEBUG

        return ret;
    }

/************** 3.Initializes the LED driver instance ****/
    self->p_led_ops_instance->pf_led_on();
    self->p_led_ops_instance->pf_led_off();

    ret = LED_OK;

    return ret;
}

/**
 * @brief Instantiates the bsp_led_driver_t target.
 * 
 * Steps:
 *  1. Adds Core interfaces into bsp_led_driver instance target.
 *  2. Adds timebase interfaces into bsp_led_driver instance target.
 *  3. Adds OS interfaces into bsp_led_driver instance target.
 *  
 * @param[in] self        : Pointer to the target of driver.
 * @param[in] led_ops     : Pointer to the led_opes_t interface.
 * @param[in] sys_time    : Pointer to the core_base_time_ms_t interface.
 * @param[in] os_delay    : Pointer to the os_delay_interface.
 * 
 * @return led_state_t : Status of the function.
 * 
 * */
led_state_t bsp_led_driver_inst(
                              bsp_led_driver_t    * const     self, 
                              led_opes_t          * const led_ops )
{
    led_state_t ret = LED_OK;
#ifdef DEBUG
    DEBUG_PRINT("bsp_led_driver_inst() called\r\n");
#endif // DEBUG

/************** 1.Checking the input params **************/
    if( NULL == self     ||
        NULL == led_ops    )
    {
#ifdef DEBUG
    DEBUG_PRINT("LED_ERROR_PARAM\r\n");
#endif // DEBUG
        ret = LED_ERROR_PARAM;
    }
   
/************** 2.Checking the resource ******************/
    if( LED_IS_INSTED == self->is_instantiated )
    {
#ifdef DEBUG
    DEBUG_PRINT("LED_ERROR_RESOURCE\r\n");
#endif // DEBUG
        ret = LED_ERROR_RESOURCE;
    }

/************** 3.Adding  interfaces *********************/
    
    // 3.1 mount external interfaces
    self->p_led_ops_instance = led_ops;

/************** 5.Initializing the LED driver instance ***/
    self->cycle_period_ms  = 0;
    self->blink_times      = 0;
    self->blink_proportion_on_off = PROPORTION_ON_OFF_X_X;

    ret = bsp_led_driver_init(self);
    if( LED_OK != ret )
    {
#ifdef DEBUG
        DEBUG_PRINT("LED init failed\r\n");
#endif // DEBUG

        self->p_led_ops_instance = NULL;

        return ret;
    }

/************** 6.Adding driver instance flag ************/
    self->is_instantiated = LED_IS_INSTED;
#ifdef DEBUG
    DEBUG_PRINT("LED driver instantiated successfully\r\n");
#endif // DEBUG

    return ret;
}

//********************************* Defines *********************************//

//********************************* Declaring *******************************//

//********************************* Declaring *******************************//
